package com.viewpagertest

import android.support.v4.view.PagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView

/**
 * Created by Administrator on 2017/6/7.
 */

class ViewPagerAdapter : PagerAdapter() {

    companion object {

        private val RES = intArrayOf(R.mipmap.image1, R.mipmap.image2, R.mipmap.image3, R.mipmap.image4)
//        private val s = arrayOf("1", "2", "3", "4")
    }

    /**
     * 是获取当前窗体界面数
     */
    override fun getCount(): Int {
        return RES.size
    }

    /**
     * 这个方法，在帮助文档中原文是could be implemented as return view == object,
     * 也就是用于判断是否由对象生成界面
     */
    override fun isViewFromObject(view: View, o: Any): Boolean {
        return view === o
    }

    /**
     * 这个方法，return一个对象，这个对象表明了PagerAdapter适配器选择哪个对象*放在当前的ViewPager中
     */
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = LayoutInflater.from(container.context).inflate(R.layout.view_pager_item, null)
        val imageView = view.findViewById(R.id.item_image) as ImageView
        imageView.setImageResource(RES[position])
        container.addView(view)
        return view
    }

    override fun getPageTitle(position: Int): CharSequence {
//        return s[position]
        when (position) {
            0 -> return "王鹏"
            1 -> return "姜语"
            2 -> return "结婚"
            3 -> return "美女"
            else -> return ""
        }
    }

    /**
     * 这个方法，是从ViewGroup中移出当前View
     */
    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }


}






























